<?php
/**
 * @name 会员分组管理控制器
 * @Author : Liuyi
 * @Created : 2016-10-28
 * @Copyright : MPE.Fiidee.Monda.Cn
 */
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;
use Workerman\Protocols\Http;

/**--tmpl-use--**/

class MemberGroupAction extends CommonAction {

	public function C_start()
	{
		parent::C_start();
	}


	protected $serviceBean="member.memberGroup.service";

    /**
     * 分组列表
     * @param HttpRequest $request
     */
    public function index(HttpRequest $request) {

        $conditions = array();
        $s_name = $request->getParameter('s_name');
        $this->assign('s_name',$s_name);
        if($s_name !== '' && $s_name!== NULL)$conditions['name'] = '%'.$s_name.'%';
        $s_s_addtime = $request->getParameter('s_s_addtime');
        $s_e_addtime = $request->getParameter('s_e_addtime');
        $this->assign('s_s_addtime',$s_s_addtime);
        $this->assign('s_e_addtime',$s_e_addtime);

        if($s_s_addtime !== '' && $s_s_addtime!== NULL){
            $conditions['addtime'] = array();
            $conditions['addtime']['>'] = $s_s_addtime.' 00:00:00';
        }


        if($s_e_addtime !== '' && $s_s_addtime!== NULL){
            if(!$conditions['addtime']){ $conditions['addtime'] = array();}
            $conditions['addtime']['<='] = $s_e_addtime.' 23:59:59';
        }
        $s_status = $request -> getParameter('s_status');
        if($s_status !== '' && $s_status!== NULL){
            $conditions['status'] = $s_status;
        }
        $this -> assign('s_status',$s_status);
        $this->setConditions($conditions);
        $page = $request -> getParameter('page');
        if( !$page || $page == "" ){
            $page = 1;
        }
        $this -> setPage($page);
        $pagesize = $request->getParameter('pagesize') ? $request->getParameter('pagesize') : $this->getPagesize();
        $this->setPagesize($pagesize);
        $service = Beans::get($this->getServiceBean());

        if($request->getParameter('export') == 1){
            $this->setPage(1);
            $this->setPagesize(2147483648);
            $ids = $request -> getParameter('ids');
            if( $ids ){
                $conditions['id']['$in'] = $ids;
                $this -> setConditions($conditions);
            }
            $exportItems = $service->getGroupItems($this->getConditions(), $this->getFields(), $this->getOrder());
            $excel = array();
            foreach($exportItems as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["name"];
                $tmp[] = $v["total"];
                $tmp[] = $v["addtime"];
                $tmp[] = $v["manager_name"];
                $tmp[] = $v["status"]==1?"启用":"禁用";
                $excel[] = $tmp;
            }
            $header = array('分组名称','会员数','创建时间','创建人','状态');
            phpexcel("分组管理",$header,$excel);
            exit();
        }
        $total = $service->count($this->getConditions());
        $items = $service->getGroupItems($this->getConditions(), $this->getFields(), $this->getOrder(),
            array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());

        $this->generatePageMenu( $total );

        //管理员信息
        $adminId = adminId();
        $adminName = adminName();

        $this->assign('pagesize',$pagesize);
        $this->assign("items", $items);
        $this->assign("adminId", $adminId);
        $this->assign("adminName", $adminName);
        $this->assign("page", $page);
        $this->setView('membergroup/index');
        $this->assign("title", "分组管理");
        $this->assign('bread', array('会员管理',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;','javascript:;'));
    }

    /**
     * 添加分组
     * @param HttpRequest $request
     */
	public function add(HttpRequest $request) {
	
		parent::add($request);
	
		$this->assign("title", "分组列表");
		$this->assign('bread', array('会员管理','分组管理',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;',url('admin/memberGroup/index'),'javascript:;'));
	
	}

    /**
     * 编辑分组数据
     * @param HttpRequest $request
     */
	public function edit(HttpRequest $request) {

        $id = $request->getParameter('id', 'trim');
        if ( empty($id) ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, parent::NO_RECORDS);
        }
        $service = Beans::get($this->getServiceBean());
        $item = $service->getItem($id);
        if ( empty($item) ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, parent::NO_RECORDS);
        }
        AjaxResult::ajaxResult(AjaxResult::OP_SUCCESS, null, $item);
	}

    /**
     * 新增分组数据
     * @param HttpRequest $request
     */
	public function insert(HttpRequest $request){
        $data = $request->getParameter('data');
        $data['addtime']=date("Y-m-d H:i:s");
        $data['manager_id']=adminId();
        if($data['status']=="")
        {
            $data['status'] = 1;
        }
        $service = Beans::get( $this -> getServiceBean() );
        if( $service -> count(array('name'=>$data['name']))>0 ){
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"分组名称已存在","");
        };
	    parent::insert($data);
    }

    /**
     * 重写父类更新数据操作
     */
    public function update(HttpRequest $request) {

        $data = $request->getParameter('data');
        //$data['manager_id'] = adminId();
        $id = $request->getParameter('id', 'trim');
        if ( empty($id) ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, parent::NO_RECORDS);
        }
        $service = Beans::get( $this -> getServiceBean() );
        if( $service -> count(array('name'=>$data['name'],'!id'=>$id))>0 ){
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"分组名称已存在","");
        };
        parent::__update($data, $id);
    }

    /**
     * 删除单条记录
     * @param HttpRequest $request
     */
    public function delete(HttpRequest $request ){
        $id = $request->getParameter('id');
        if ( $id == "" ) AjaxResult::ajaxResult('error', COM_ERR_MSG);
        $service = Beans::get($this->getServiceBean());
        if( !$service -> isEmptyGroup($id) ){
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"该分组已关联会员",null);
        }
        if ( $service->delete($id) ) {
            AjaxResult::ajaxSuccessResult();
        } else {
            AjaxResult::ajaxFailtureResult();
        }
    }

    /**
     * 启用/禁用
     * @param HttpRequest $request
     */
    public function set(HttpRequest $request){
        $field = $request->getParameter('field', 'trim');
        $value = $request->getParameter('value', 'trim');
        $id = $request->getParameter('id', 'trim');
        if ( empty($id) || empty($field) ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, self::NO_RECORDS);
        }
        $service = Beans::get($this->getServiceBean());
        if( !$service -> isEmptyGroup($id) ){
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"该分组已关联会员",null);
        }
        if ( $service->set($field, $value, $id) ) {
            AjaxResult::ajaxSuccessResult();
        } else {
            AjaxResult::ajaxFailtureResult();
        }
    }

    /**
     * 批量删除
     * @param HttpRequest $request
     */
    public function deletes(HttpRequest $request ){
        $ids = $request->getParameter('ids');
        //var_dump($ids);die;
        if ( empty($ids) ) AjaxResult::ajaxResult('error', COM_ERR_MSG);
        $service = Beans::get($this->getServiceBean());
        if( !$service -> isEmptyGroup($ids) ){
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"所选分组已关联会员",null);
        }
        if ( $service->deletes(array('id' => array('$in' => $ids) ))) {
            AjaxResult::ajaxSuccessResult();
        } else {
            AjaxResult::ajaxFailtureResult();
        }
    }

    /**
     * 批量启用/禁用
     * @param HttpRequest $request
     */
    public function do_updateStatus(HttpRequest $request){
        $type = $request -> getParameter('type');
        if( $type=="" ){
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"操作失败",null);
        }
        $ids = $request->getParameter('ids');
        if ( empty($ids) ) AjaxResult::ajaxResult('error', COM_ERR_MSG);
        $service = Beans::get($this->getServiceBean());
        if( !$service -> isEmptyGroup($ids) ){
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"所选分组已关联会员",null);
        }
        if( $type == 1 ){
            $service->updates(array('status'=>1),array('id' => array('$in' => $ids)));
            AjaxResult::ajaxSuccessResult();
        }else{
            $service->updates(array('status'=>0),array('id' => array('$in' => $ids)));
            AjaxResult::ajaxSuccessResult();
        }

    }
}
